type PayRes = {
  /**
   * 200 =>成功
   * 300 =>等待
   * 500 =>失败
   */
  code: number;
  msg: string;
  url?: string;
};

export default async function showPayPage(data: any, payType: string) {
  if (payType === PayType.WECHATJSPAY) {
    return wechatJsPay(data);
  } else if (payType === PayType.ALIPAGE) {
    return {
      code: 300,
      msg: '正在支付',
      title: '收银台',
      url: data.payParam,
    };
  }
  return {
    code: 500,
    msg: '暂不支持该支付方式',
  };
}

function wechatJsPay(data: any) {
  return new Promise<PayRes>((resolve, reject) => {
    // @ts-ignore
    WeixinJSBridge.invoke(
      'getBrandWCPayRequest',
      {
        appId: data.appId, //动态获取初始化请求中返回的pay_info参数中appId值
        timeStamp: data.timeStamp, //动态获取初始化请求中返回的pay_info参数中timeStamp值
        nonceStr: data.nonceStr, //动态获取初始化请求中返回的pay_info参数中nonceStr值
        package: data.packageValue, //动态获取初始化请求中返回的pay_info参数中package值
        signType: data.signType, //动态获取初始化请求中返回的pay_info参数中signType值
        paySign: data.paySign,
      },

      function (res: any) {
        if (res.err_msg === 'get_brand_wcpay_request:ok') {
          resolve({ code: 200, msg: '支付成功' });
        } else {
          resolve({ code: 500, msg: '支付失败' });
        }
      }
    );
  });
}

/// 支付类型
export class PayType {
  // 支付宝h5支付
  static ALIPAGE = 'aliPage';
  // 微信h5支付
  static WECHATJSPAY = 'wechatJsPay';
}
